package cn.toleo.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.toleo.common.pojo.SearchResult;
import cn.toleo.search.dao.SearchDao;
import cn.toleo.search.service.SearchService;

/**
 * 搜索服务功能实现
 * @author Administrator
 *
 */
@Service
public class SearchServiceImpl implements SearchService{
	@Autowired
	private SearchDao searchDao;

	@Override
	public SearchResult search(String queryString, int page, int rows) throws Exception{
		//主干输入修饰
		SolrQuery query = new SolrQuery();
		query.setQuery(queryString);
		if(page<1) page=1;
		query.setStart((page-1)*rows);
		if(rows<1) rows=10;
		query.setRows(rows);
		query.set("df", "item_title");
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<font color='red'>");
		query.setHighlightSimplePost("</font>");
		
		//主干
		SearchResult searchResult=searchDao.search(query);
		
		//主干输出修饰
		long recordCount = searchResult.getRecordCount();
		long pages=recordCount/rows;
		if(recordCount%rows>0){
			pages++;
		}
		searchResult.setTotalPages(pages);
		
		//结果
		return searchResult;
	}

}
